import random

import pandas as pd
import numpy as np
from utils import BuildName, PhoneNOGenerator, NationGenerator
import json

def get_industry():
    data = pd.read_excel('./industry.xls')
    pass

    keep_col = ['industry_id', 'name', 'description']
    big_category = data[np.isnan(data['parent_id'])]
    big_category[keep_col].to_csv('./big_category.csv', index=False, header=False, sep=' ')


def get_institute():
    """
    # 导入学院实体
    :return:
    """
    institute = pd.read_excel('/home/myh/PycharmProjects/jishe/data/2010届-2019届.xls')
    institute = institute['学院'].drop_duplicates()
    institute.to_csv('./institute.csv', index=False, header=False)
    pass


def get_major():
    """
    # 导入学院实体
    :return:
    """
    data_df = pd.read_excel('/home/myh/PycharmProjects/jishe/data/2010届-2019届.xls')
    major = data_df[''].drop_duplicates()
    major.to_csv('./major.csv', index=False, header=False)
    pass


def get_classes():
    """
    # 导入学院实体
    :return:
    """
    data_df = pd.read_excel('/home/myh/PycharmProjects/jishe/data/2010届-2019届.xls')
    major = data_df['班级'].drop_duplicates()
    major.to_csv('./classes.csv', index=False, header=False)
    pass


def get_rel(keep_col=['学院', '专业'], file_name='./institute_major.csv'):
    data_df = pd.read_excel('/home/myh/PycharmProjects/jishe/data/2010届-2019届.xls')
    institute_major_df = data_df[keep_col]  # 保留需要的
    institute_major_df = institute_major_df[~institute_major_df[keep_col[0]].isin(['XX', 'xx'])]  # 简单数据清晰
    institute_major_df.to_csv(file_name, header=False, index=False, sep=' ')  # 保存文件



def get_students():
    """
    获取
    学号，
    班级，
    入学时间，
    培养层次，

    姓名（随机生成），
    性别：随机生成
    号码：随机生成
    行业号码 1～96,
    头像： 随机生成，
    生日：入学日期 - 22年，随机一个数字。
    民族：随机生成，
    所在地：随机生成，
    生源地：随机生成，
    :return:
    """
    data_df = pd.read_excel('/home/myh/PycharmProjects/jishe/data/2010届-2019届.xls')
    keep_col = ['学号',  '班级', '入学日期', '培养层次']
    studentid_cloassname_df = data_df[keep_col]
    # 修改index
    studentid_cloassname_df.columns = ['student_id','class_name','attendance_date', 'level']
    #
    studengt_list = studentid_cloassname_df.to_dict('records')
    namecreator = BuildName()
    phoneenerator = PhoneNOGenerator()
    nation_generator = NationGenerator()
    img_root = "http://121.196.107.223:8000/img/" # 0 ~ 129
    def build_student(student_dict):
        GENDER_STATUS = ['0', '1']
        student_dict['name'] = namecreator.create_name()
        student_dict['gender'] = random.choices(GENDER_STATUS)[0]
        student_dict['mobile'] = phoneenerator.phoneNORandomGenerator()
        student_dict['nation'] = nation_generator.get_nation()
        student_dict['image'] = img_root + str(random.randint(0,129)) + '.jpeg'

        year, month, day = student_dict.get('attendance_date').split('-')
        year = int(year)
        year -= random.randint(23,25)
        year = str(year)
        month = str(random.randint(1,12))
        day = str(random.randint(1,28))
        student_dict['birthday'] = '-'.join([year, month, day])

        student_dict['industry_id'] = random.randint(1, 96)
        return student_dict

    studengt_list = map(build_student,studengt_list)
    #
    studengt_list = list(studengt_list)
    with open('./students1.json', mode='w', encoding='utf-8') as f:
        json.dump({'data': studengt_list[:30000]}, f, ensure_ascii=False)

    with open('./students2.json', mode='w', encoding='utf-8') as f:
        json.dump({'data': studengt_list[30000:]}, f, ensure_ascii=False)
    # student_df = pd.DataFrame(list(studengt_list))
    # student_df.to_csv('./students.csv')



def main():
    # get_industry()
    # get_major()
    # get_classes()
    # get_institute_major()
    # get_rel(['专业', '班级'], './major_classes.csv')
    # get_rel(['学号', '班级'], './major_classes.csv')
    get_students()
    pass


if __name__ == '__main__':
    main()
